""" author:Silent
    contact: 1343693670@qq.com
    datetime:2022/12/2 21:36
    software: PyCharm"""
from django.utils.deprecation import MiddlewareMixin


class CORSMiddleware(MiddlewareMixin):

    def process_respose(self, request, response):
        # 添加响应头
        # 允许相应的域名来访问，多个域名中间以逗号隔开，如果全部可使用'*'
        response['Access-Control-Allow-Origin'] = "*"
        if request.method == "OPTIONS":
            # 允许携带的请求头，多个中间以逗号隔开
            # 复杂请求会先发预检
            response['Access-Control-Allow-Headers'] = "Content-Type"
            # 允许发送的请求方式
            response['Access-Control-Allow-Methods'] = "DELETE,PUT,PATCH"
        return response
